package com.genius.unite_platform.device.service.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.FillAddJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.unite_platform.device.entity.DeviceEntity;
import com.genius.unite_platform.device.service.DeviceServiceAdapter;
import com.genius.unite_platform.notice_info.entity.Notice_infoEntity;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;


@Service("DeviceEditService")
public class DeviceEditServiceImpl extends DeviceServiceAdapter{

	@Override
	public JSONObject edit(JSONObject json, JSONObject response,HttpSession session) {
    	String updateField = DeviceEntity.devicename+","+DeviceEntity.memo+","+DeviceEntity.lrr+","+DeviceEntity.lrsj+"=now()";
    	String whereField = DeviceEntity.deviceno;
    	JSONObject userJson = (JSONObject)session.getAttribute(SessionJson.currentUser);			
		json.put(DeviceEntity.lrr, userJson.getString(UserJson.name));		
		
    	try {
    		JSONArray Arrayadd = new JSONArray();
    		JSONArray Arrayupdate = new JSONArray();   
    		JSONArray delData = new JSONArray();    		
    		int  totalnum=Integer.parseInt(json.getString("totalNum")); 
    		System.out.println(totalnum);
			for(int i=0;i<totalnum;i++){
				JSONObject jsonObject = new JSONObject();				
				String flag=json.getString("flag_"+i);				
				String   typeid=json.getString("type_"+i);
				String   typename="";
			    if (typeid.equals("1"))
				{
					typename="公司";
				}
				if (typeid.equals("2"))
				{
					typename="厂家";
				}
				if (typeid.equals("3"))
				{
					typename="自有";
				}	
				
				Date day=new Date();
				SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		
				jsonObject.put("deviceno",json.getString("deviceno"));
				jsonObject.put("modelId",json.getString("id_"+i));
				jsonObject.put("modelname",json.getString("name_"+i));		
				jsonObject.put("modeltypeId",typeid);	
				jsonObject.put("modeltype",typename);
				jsonObject.put("imgs",json.getString("img_"+i));	
				jsonObject.put("lrr",userJson.getString(UserJson.name));	
				jsonObject.put("lrsj",sdf.format(day));					
				
				
				if (flag.equals("del"))
				{
					delData.add(jsonObject) ;
				}
				if (flag.equals("UID"))
				{
					Arrayupdate.add(jsonObject) ;
				}
				if (flag.equals("add"))
				{
					System.out.println(json.getString("id_"+i));
					if (json.getString("id_"+i)!="")
					{
					   Arrayadd.add(jsonObject) ;	 
					}
				}
			}
			    
			System.out.println(delData);
			System.out.println(Arrayupdate);
			System.out.println(Arrayadd);
    		//1.删除    	
    		DBTools.deleteBach("deviceno,modelId", "devicemodel",delData); 	
    		
    		//2.修改
    		String itemField = "modelname,modeltypeId,modeltype,imgs,lrr,lrsj";
    		DBTools2.updateBatch(itemField, "deviceno,modelId", "devicemodel", Arrayupdate);
    		
    		//3.新增
    		String insertfiled = "deviceno,modelId,modelname,modeltypeId,modeltype,imgs,lrr,lrsj";
			DBTools2.insertBatch(insertfiled,"devicemodel",Arrayadd);	    		
	        
			//4.修改总表
			String updateTable = "device";
			DBTools2.update(updateField, whereField, updateTable,json);
			
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Success);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
		}
		return response;
	}
	@Override
	public JSONObject fillEdit(JSONObject json, JSONObject response) {   
	try {
    		JSONObject addJson = new JSONObject();
    		
    		
    		String selectField = "";
    		String whereField = "deviceno";
    		String table = "device";
			HashMap<String, String> sqlMap = new HashMap<>();
			
			
			JSONObject fillJson = new JSONObject();
			
			addJson = DBTools.getFilling(selectField,whereField,table,json,addJson);
			JSONObject fillEditJson = DBTools.getFilling(sqlMap);
			fillJson.put(FillAddJson.field, addJson);
			fillJson.put(FillAddJson.select, fillEditJson);
			
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Add_Data_Success);
			response.put(ResponseJson.data,fillJson);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Connect_Failed);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Fill_Data_Error);
		}
		return response;
	}
	
	
	@Override
	public JSONArray getDeviceModelList(String deviceno) {
		String sql = "select * from devicemodel where deviceno = '"+deviceno+"'";
	    JSONArray jsonArray = new JSONArray();
        try {
            jsonArray = DBUtils.selectArr(sql,null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return jsonArray;
	}
}

